{{- $file := .file -}}
{{- $imagesDir := .Page.Site.Params.imagesDir -}}
{{- $alt := .alt -}}
{{- if not $alt -}}
  {{- $alt = .Text -}}
{{- end -}}
{{- $cap := .cap -}}
{{- $scratch := newScratch -}}
{{- $scratch.Set "classes" "image_figure" -}}

{{/* Add classes for image type */}}
{{- $typeClass := "" -}}
{{- with .type -}}
  {{- $typeClass = printf "%s" . | printf "%s%s" " image_" | printf "%s" -}}
{{- end -}}
{{- $scratch.Add "classes" $typeClass -}}

{{/* Determine if page bundles are set. */}}
{{- $bundle := .Page.Site.Params.usePageBundles -}}
{{- if eq .Page.Params.usePageBundles false -}}
  {{- $bundle = false -}}
{{- end -}}
{{- if eq .Page.Params.usePageBundles true -}}
  {{- $bundle = true -}}
{{- end -}}

{{/* Determine source of image and get attributes. */}}
{{- $image := "" -}}
{{- if strings.HasPrefix $file "http" -}}
  {{- $scratch.Add "classes" " image_external" -}}
  {{- $image = resources.GetRemote $file -}}
  {{- with $image }}
    {{- if eq $image.MediaType.MainType "image" -}}
      {{- if eq $image.MediaType.SubType "svg" -}}
        {{- $image = "" -}}
        {{- $scratch.Add "classes" " image_svg" -}}
      {{- else -}}
        {{- $file = path.Join $imagesDir $image -}}
        {{- $image = $image.Content | resources.FromString $file -}}
      {{- end -}}
    {{- else -}}
      {{- $image = "" -}}
    {{- end -}}
  {{- end -}}
{{- else -}}
  {{- $scratch.Add "classes" " image_internal" -}}
  {{ $file = (path.Clean $file) }}
  {{- if eq $bundle true -}}
    {{ $image = .Resources.GetMatch $file }}
  {{- end -}}
{{- end -}}

{{- partial "common/figure" (
  dict
    "file" $file
    "image" $image
    "diskPath" $.Page.File.Dir
    "webPath" $.Page.RelPermalink
    "alt" $alt
    "cap" $cap
    "classes" ($scratch.Get "classes")
    "bundle" $bundle
) -}}
